package com.dbf.forum.controller.offre;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.dbf.forum.Constants;
import com.dbf.forum.controller.produit.ProduitController;
import com.dbf.forum.core.offre.OffreDto;
import com.dbf.forum.core.produit.ProduitDto;
import com.dbf.forum.service.offre.OffreService;


@RestController
@RequestMapping(value = Constants.URI_API + Constants.URI_OFFRES)
public class OffreController {
	
	private static final Logger log = LoggerFactory
            .getLogger(ProduitController.class);
	
	@Inject
	OffreService offreService;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Page<OffreDto>> getOffresByUserAndProduits(
            @PageableDefault(page = 0, size = 10, sort = "createdDate", direction = Direction.DESC) Pageable page) {
		if (log.isDebugEnabled()) {
            log.debug("get all posts of q@, page@" + page);
        }
        Page<OffreDto> offreDtos = offreService.getOffresByUserAndProduits(page);

        if (log.isDebugEnabled()) {
            log.debug("get posts size @" + offreDtos.getTotalElements());
        }

        return new ResponseEntity<>(offreDtos, HttpStatus.OK);
    }

 	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<OffreDto> getOffre(@PathVariable("id") Long id) {
        if (log.isDebugEnabled()) {
            log.debug("get postsinfo by id @" + id);
        }

        OffreDto offreDto = offreService.getOffreById(id);

        if (log.isDebugEnabled()) {
            log.debug("get post @" + offreDto);
        }

        return new ResponseEntity<>(offreDto, HttpStatus.OK);
    }
 	

 	@RequestMapping(value = "/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<OffreDto> saveOffre(@PathVariable("id") Long id, @RequestBody ProduitDto produitDto) {
        if (log.isDebugEnabled()) {
            log.debug("get postsinfo by id @" + id);
        }
        
        
        OffreDto offreDto = offreService.getOffreById(id);
        offreDto.setProduitDto(produitDto);
//        offreService.updateOffre(produit)
//        if (log.isDebugEnabled()) {
//            log.debug("get post @" + offreDto);
//        }

        return new ResponseEntity<>(offreDto, HttpStatus.OK);
    }
}
